const gulp = require("gulp");
const cssmin=require('gulp-cssmin')
const sass=require('gulp-sass')(require('sass'))
const uglify=require('gulp-uglify')
const babel=require('gulp-babel')
const htmlmin=require('gulp-htmlmin')
const clean=require('gulp-clean')
const webserver=require('gulp-webserver')
gulp.task('default',function(){
    console.log('执行defailt任务>>>');
})

gulp.task('copy',function(){
    return gulp.src('./src/css/index.css').pipe(gulp.dest('./dist/css/'))
})

gulp.task('css',function(){
        return gulp.src('./src/sass/*.scss').pipe(sass()).pipe(cssmin()).pipe(gulp.dest('./dist/css'))
})
gulp.task('js',function(){
        return gulp.src('./src/js/**')
               // .pipe(babel({
               //     presets:['es2015']
               // }))
               // .pipe(uglify())
                .pipe(gulp.dest('./dist/js'))
})

gulp.task('html',function(){
        return gulp.src('./src/pages/**')
                    .pipe(htmlmin({
                        removeEmptyAttributes:true, 
                        collapseWhitespace:true
                    }))
                    .pipe(gulp.dest('./dist/pages'))
})
gulp.task('img',function(){
    return gulp.src('./src/img/**').pipe(gulp.dest('./dist/img'))
})
gulp.task('lib',function(){
    return gulp.src('./src/lib/**').pipe(gulp.dest('./dist/lib'))
})
gulp.task('clean',function(){
        return gulp
                .src('./dist',{allowEmpty:true})
                .pipe(clean())
})

gulp.task('webserver',function(){
        return gulp
                    .src('./dist')
                    .pipe(webserver({
                        host:'localhost',
                        port:3000,
                        livereload:true,
                        open:'../pages/index.html'
                    }))
})

gulp.task('watch',function(){
    gulp.watch('./src/sass/**',gulp.parallel('css'))
    gulp.watch('./src/js/**',gulp.parallel('js'))
    gulp.watch('./src/pages/**',gulp.parallel('html'))
    gulp.watch('./src/lib/*/**',gulp.parallel('lib'))
    gulp.watch('./src/img/**',gulp.parallel('img'))
})

exports.default=gulp.series('clean',
        gulp.parallel('js','css','html','lib','img'),
        gulp.series('webserver','watch')
    )